<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .wrapper {
            width: 600px;
            margin: 50px auto;
        }

        .nav {
            display: flex;
            line-height: 40px;
            text-align: center;
        }

        .nav li {
            flex: 1;
            border: 1px solid #333;
        }

        .nav li+li {
            border-left: 0;
        }

        .active {
            background-color: pink;
            color: #fff;
        }

        .zone-list {
            height: 400px;
            border: 1px solid #333;
            border-top: 0;
        }

        .zone-list .zone-item {
            height: 400px;
            text-align: center;
            line-height: 400px;
            display: none;
        }

        .zone-list .show {
            display: block;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <ul class="nav">
            <li class="active">nav_01</li>
            <li>nav_02</li>
            <li>nav_03</li>
            <li>nav_04</li>
            <li>nav_05</li>
            <li>nav_06</li>
        </ul>
        <div class="zone-list">
            <div class="zone-item show">01</div>
            <div class="zone-item">02</div>
            <div class="zone-item">03</div>
            <div class="zone-item">04</div>
            <div class="zone-item">05</div>
            <div class="zone-item">06</div>
        </div>
    </div>
</body>
<script>
    var navItems = document.querySelectorAll(".nav li");
    var zoneItems = document.querySelectorAll(".zone-list .zone-item");
    console.log(navItems);


    // 点击li => 当前导航添加活跃状态的样式  其他的所有同级li清除活跃状态的样式

    // 方法2:
    // 我也不想记录之前哪一个导航被点击过,索性全都清除,再把自己变为活跃状态

    // 页面加载时 ==> 循环绑定事件
    for (let i = 0; i < navItems.length; i++) {
        let li = navItems[i];

        let zone = zoneItems[i];
        zone.style.backgroundColor = randColor();
        li.onclick = function () {

            // 先清除所有导航的活跃状态的样式 (点击导航时 遍历清除每个元素的活跃样式)
            for (var j = 0; j < navItems.length; j++) {
                navItems[j].className = "";
                zoneItems[j].classList.remove("show");
            }

            // 当前被点击的元素变为活跃状态
            this.className = "active"; //只用设置一次class名
            zoneItems[i].classList.add("show");
        }
    }




    function randColor() {
        var str = "0123456789abcdef";
        var colorStr = "#";
        for (var i = 0; i < 6; i++) {
            var index = Math.floor(Math.random() * str.length);
            var char = str.charAt(index);
            colorStr += char;
        }
        return colorStr;
    }





</script>

</html>